//
//  ScanInstallBoxVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/28.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "ScanTurnOverBoxVC.h"
#import "TurnOverBoxModel.h"
#import "ScanBoxCell.h"
#import "AddTurnOverBoxCell.h"
#import "TurnOverBoxCell.h"
#import "ScanTurnOverFullBoxVC.h"

@interface ScanTurnOverBoxVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <TurnOverBoxModel *>*boxArray;

@end

@implementation ScanTurnOverBoxVC

-(NSMutableArray<TurnOverBoxModel *> *)boxArray {
    if (!_boxArray) {
        _boxArray = [NSMutableArray array];
    }
    return _boxArray;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setTitle:@"确认提交" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(commitClick) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:nextBtn];
    }
    return _bottomView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - self.bottomView.nim_height - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}


//确认提交
- (void)commitClick {
    ScanTurnOverFullBoxVC *vc = [ScanTurnOverFullBoxVC new];
    vc.boxModel = self.boxModel;
    [self.boxArray enumerateObjectsUsingBlock:^(TurnOverBoxModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.boxNO = [NSString stringWithFormat:@"2317983629%lu",(unsigned long)idx];
    }];
    vc.boxArray = self.boxArray;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"扫码装箱";
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return self.boxArray.count + 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanBoxCell *cell = [ScanBoxCell ScanBoxCellWithTableView:tableView];
        cell.model = self.boxModel;
        return cell;
    }else {
        if (indexPath.row < self.boxArray.count) {
            TurnOverBoxCell *cell = [TurnOverBoxCell TurnOverBoxCellWithTableView:tableView];
            cell.model = self.boxArray[indexPath.row];
            __weak_self;
            cell.deleteBlock = ^{
                [weakself.boxArray removeObjectAtIndex:indexPath.row];
                [tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationFade];
            };
            cell.scanBlock = ^{
                
            };
            return cell;
        }else {
            AddTurnOverBoxCell *cell = [AddTurnOverBoxCell AddTurnOverBoxCellWithTableView:tableView];
            __weak_self;
            cell.addBlock = ^{
                TurnOverBoxModel *model = [TurnOverBoxModel new];
                [weakself.boxArray addObject:model];
                [tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
            };
            return cell;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanBoxCell *cell = [ScanBoxCell ScanBoxCellWithTableView:tableView];
        return cell.height;
    }else {
        if (indexPath.row < self.boxArray.count) {
            TurnOverBoxCell *cell = [TurnOverBoxCell TurnOverBoxCellWithTableView:tableView];
            return cell.height;
        }else {
            AddTurnOverBoxCell *cell = [AddTurnOverBoxCell AddTurnOverBoxCellWithTableView:tableView];
            return cell.height;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ScanBoxCell *cell = [ScanBoxCell ScanBoxCellWithTableView:tableView];
        return cell.height;
    }else {
        if (indexPath.row < self.boxArray.count) {
            TurnOverBoxCell *cell = [TurnOverBoxCell TurnOverBoxCellWithTableView:tableView];
            return cell.height;
        }else {
            AddTurnOverBoxCell *cell = [AddTurnOverBoxCell AddTurnOverBoxCellWithTableView:tableView];
            return cell.height;
        }
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenW, 27)];
        view.backgroundColor = Color_backgroundColor;
        UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, 100, 17)];
        titleLab.text = @"周转箱编号";
        titleLab.textColor = HexColor(0x333333);
        titleLab.font = BoldFontOfSize(16);
        [view addSubview:titleLab];
        
        UILabel *desc1Lab = [UILabel new];
        desc1Lab.text = @"已装入";
        desc1Lab.font = [UIFont systemFontOfSize:12];
        desc1Lab.textColor = HexColor(0x9C9DB3);
        [desc1Lab sizeToFit];
        
        UILabel *desc2Lab = [UILabel new];
        desc2Lab.text = @"个周转箱";
        desc2Lab.font = [UIFont systemFontOfSize:12];
        desc2Lab.textColor = HexColor(0x9C9DB3);
        [desc2Lab sizeToFit];
        
        UILabel *countLab = [UILabel new];
        countLab.text = [NSString stringWithFormat:@"%lu",(unsigned long)self.boxArray.count];
        countLab.font = [UIFont systemFontOfSize:12];
        countLab.textColor = HexColor(0x4AD879);
        [countLab sizeToFit];
        
        desc2Lab.nim_left = ScreenW - 15 - desc2Lab.nim_width;
        countLab.nim_left = desc2Lab.nim_left - countLab.nim_width;
        desc1Lab.nim_left = countLab.nim_left - desc1Lab.nim_width;
        desc1Lab.nim_centerY = titleLab.nim_centerY;
        countLab.nim_centerY = titleLab.nim_centerY;
        desc2Lab.nim_centerY = titleLab.nim_centerY;
        
        [view addSubview:desc2Lab];
        [view addSubview:countLab];
        [view addSubview:desc1Lab];
        
        return view;
    }
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 27;
    }
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
